package com.example.dellpc.myapplication;

import android.app.Activity;
import android.content.Context;
import android.nfc.Tag;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.db.annotation.Column;
import com.lidroid.xutils.db.annotation.Id;
import com.lidroid.xutils.db.annotation.Table;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by 刘智强 on 2016/8/17.
 */

public class XUtilsActivity extends Activity {

    private String TAG = XUtilsActivity.class.getName();
    private DbUtils dbUtils;
    @ViewInject(R.id.btn_add)
    private Button btn_add;
    @ViewInject(R.id.btn_select)
    private Button btn_select;
    @ViewInject(R.id.btn_update)
    private Button btn_update;
    @ViewInject(R.id.btn_delete)
    private Button btn_delete;
    @ViewInject(R.id.et_name)
    private EditText et_name;
    @ViewInject(R.id.et_age)
    private EditText et_age;
    @ViewInject(R.id.listview)
    private ListView listView;
    @ViewInject(R.id.et_id)
    private EditText et_id;
    @ViewInject(R.id.pb)
    private ProgressBar pb;
@ViewInject(R.id.relayout)
    private RelativeLayout relayout;

    private int limit = 3;

    private int start = 0;

    List<User> datas = new ArrayList<>();
    private HttpUtils httpUtils;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            start = start + limit;
            initDatas(start);
            pb.clearAnimation();
            relayout.setVisibility(View.GONE);
            listView.setSelection(start);//必须先清楚动画 才来隐藏progressbar
            Utils.toast(getApplicationContext(), "加载完成...");
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xutilsactivity);
        ViewUtils.inject(this);
        initDBUtils(getApplicationContext());
        initDatas(start);
    }

    private void initDBUtils(Context context) {
        dbUtils = DbUtils.create(context);
        try {
            dbUtils.createTableIfNotExist(User.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.btn_add, R.id.btn_select, R.id.btn_update, R.id.btn_delete})
    public void buttonClicked(Button btn) {
        String name = et_name.getText().toString();
        String age = et_age.getText().toString();
        String id = et_id.getText().toString();
        switch (btn.getId()) {
            case R.id.btn_add:
                try {
                    if (!name.isEmpty() && !age.isEmpty()) {
                        User user = new User();
                        user.setName(name);
                        user.setAge(Integer.valueOf(age));
                        dbUtils.save(user);
                    } else {
                        btn_add.setClickable(false);
                    }
                    Utils.toast(getApplicationContext(),"添加成功");

                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_select:
                Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
                        this, R.anim.loading_animation);
                pb.setAnimation(hyperspaceJumpAnimation);
                pb.startAnimation(hyperspaceJumpAnimation);
                relayout.setVisibility(View.VISIBLE);

                new Thread() {
                    @Override
                    public void run() {

                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.sendEmptyMessage(0);
                    }
                }.start();

                break;
            case R.id.btn_update:

                try {

                    if (!name.isEmpty() && !age.isEmpty() && !id.isEmpty()) {
                        User user1 = new User();
                        user1.setId(Integer.parseInt(id));
                        user1.setAge(Integer.valueOf(age));
                        user1.setName(name);
                        dbUtils.update(user1, new String[]{"name", "age"});
                        Utils.toast(this, "修改成功");
                    } else {
                        btn_update.setClickable(false);
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;
            default:
        }

    }

    public void initDatas(int start) {
        List<User> users = null;
        try {
            users = dbUtils.findAll(Selector.from(User.class).limit(limit).offset(start));
            if (users != null && users.size() == 0) {
                Utils.toast(getApplicationContext(), "没有更多数据了...");
            } else if (users != null) {
                datas.addAll(users);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        ArrayAdapter<User> adapter = new ArrayAdapter<User>(getApplicationContext(), R.layout.support_simple_spinner_dropdown_item, datas);
        listView.setAdapter(adapter);


    }

    @Table(name = "user")// 表名
    public static class User {
        @Id(column = "id") //id列
        private int id;
        @Column(column = "name")// 列名
        private String name;
        @Column(column = "age")// 年龄
        private int age;

        public User() {
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getId() {
            return id;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }


}
